<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Rotate XY</title>
    <link rel="stylesheet" href="../include/style.css">
  </head>
  <body>
    <header>
      Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
    </header>
    <canvas id="canvas" width="400" height="400"></canvas>
    <aside>Move mouse on canvas element.</aside>

    <script src="../include/utils.js"></script>
    <script src="./classes/ball3d.js"></script>
    <script>
    window.onload = function () {
      var canvas = document.getElementById('canvas'),
          context = canvas.getContext('2d'),
          mouse = utils.captureMouse(canvas),
          balls = [],
          numBalls = 50,
          fl = 250,
          vpX = canvas.width / 2,
          vpY = canvas.height / 2,
          angleX, angleY;

      for (var ball, i = 0; i < numBalls; i++) {
        ball = new Ball3d(15);
        ball.xpos = Math.random() * 200 - 100;
        ball.ypos = Math.random() * 200 - 100;
        ball.zpos = Math.random() * 200 - 100;
        balls.push(ball);
      }

      function rotateX (ball, angle) {                      
        var position = [ball.xpos, ball.ypos, ball.zpos],
            sin = Math.sin(angle),
            cos = Math.cos(angle),
            xRotMatrix = [];
        
        xRotMatrix[0] = [1,    0,   0];
        xRotMatrix[1] = [0,  cos, sin];
        xRotMatrix[2] = [0, -sin, cos];
        
        var result = matrixMultiply(position, xRotMatrix);
        ball.xpos = result[0];
        ball.ypos = result[1];
        ball.zpos = result[2];
      }
      
      function rotateY (ball, angle) {
        var position = [ball.xpos, ball.ypos, ball.zpos],
            sin = Math.sin(angle),
            cos = Math.cos(angle),
            yRotMatrix = [];
        
        yRotMatrix[0] = [ cos, 0, sin];
        yRotMatrix[1] = [   0, 1,   0];
        yRotMatrix[2] = [-sin, 0, cos];
        
        var result = matrixMultiply(position, yRotMatrix);
        ball.xpos = result[0];
        ball.ypos = result[1];
        ball.zpos = result[2];
      }

      function matrixMultiply (matrixA, matrixB) {
        var result = [];
        result[0] = matrixA[0] * matrixB[0][0] +
                    matrixA[1] * matrixB[1][0] +
                    matrixA[2] * matrixB[2][0];

        result[1] = matrixA[0] * matrixB[0][1] +
                    matrixA[1] * matrixB[1][1] +
                    matrixA[2] * matrixB[2][1];

        result[2] = matrixA[0] * matrixB[0][2] +
                    matrixA[1] * matrixB[1][2] +
                    matrixA[2] * matrixB[2][2];
        return result;
      }

      function setPerspective (ball) {
        if (ball.zpos > -fl) {
          var scale = fl / (fl + ball.zpos);
          ball.scaleX = ball.scaleY = scale;
          ball.x = vpX + ball.xpos * scale;
          ball.y = vpY + ball.ypos * scale;
          ball.visible = true;
        } else {
          ball.visible = false;
        }
      }

      function move (ball) {
        rotateX(ball, angleX);
        rotateY(ball, angleY);
        setPerspective(ball);
      }

      function zSort (a, b) {
        return (b.zpos - a.zpos);
      }
      
      function draw (ball) {
        if (ball.visible) {
          ball.draw(context);
        }
      }

      (function drawFrame () {
        window.requestAnimationFrame(drawFrame, canvas);
        context.clearRect(0, 0, canvas.width, canvas.height);

        angleX = (mouse.y - vpY) * 0.001;
        angleY = (mouse.x - vpX) * 0.001;
        balls.forEach(move);
        balls.sort(zSort);
        balls.forEach(draw);
      }());
    };
    </script>
  </body>
</html>
